package game.spider.peri;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.MotionEvent;
import net.ds0101.framework.android.neox.action.NXPlay;
import net.ds0101.framework.android.neox.action.NXRepeatForever;
import net.ds0101.framework.android.neox.core.NXMenuItemActionHandler;
import net.ds0101.framework.android.neox.core.NXNode;
import net.ds0101.framework.android.neox.main.NXDirector;
import net.ds0101.framework.android.neox.main.NXScene;
import net.ds0101.framework.android.neox.widget.NXBitmapSheet;
import net.ds0101.framework.android.neox.widget.NXMenu;
import net.ds0101.framework.android.neox.widget.NXMenuItem;
import net.ds0101.framework.android.neox.widget.NXMenuItemImage;
import net.ds0101.framework.android.neox.widget.NXMenuItemSprite;
import net.ds0101.framework.android.neox.widget.NXSprite;

public class GameScene extends NXScene {
	private static final int SPRITE_MAX = 20;
	private static final int MENU_ITEM_NUM = 4;
	private static final int MAX_START_RECT_SIZE = 80;
	private Monster[] mMonster = null;
	private Bitmap[] mMenuSkin;
	private NXMenuItem[] mDirectionKey;
	private NXMenu mDirectionTray;
	private LeadingRole mSpider;
	private static Bitmap mMonsterTexture;
	private static Bitmap mSpiderTexture;
	private Region mLeadingRoleTerritory = new Region();
	private volatile boolean mScreenTouched;
	private Rect mStartRect = new Rect();
	private Paint mPaint = new Paint();

	public GameScene(Context context) {
		super(context);
		mMenuSkin = new Bitmap[MENU_ITEM_NUM];
		for (int i = 0; i < MENU_ITEM_NUM; ++i) {
			mMenuSkin[i] = 
				((BitmapDrawable)getResources().getDrawable(R.drawable.icon)).getBitmap();
		}
		
		mMonster = new Monster[SPRITE_MAX];
		if (null == mMonsterTexture) {
			mMonsterTexture = 
				((BitmapDrawable)getResources().getDrawable(R.drawable.monster02)).getBitmap();
		}
		if (null == mSpiderTexture) {
			mSpiderTexture = 
				((BitmapDrawable)getResources().getDrawable(R.drawable.spider)).getBitmap();
		}
	}
	
	public void onEnter() {
		super.onEnter();
		Point []itemPos = new Point[] {
				new Point(0, 0),
				new Point(50, 50),
				new Point(100, 0),
				new Point(50, -50),
				};
		
		mSpider = new LeadingRole(mSpiderTexture, 3, 4);
		mSpider.position(5, 5);
		NXBitmapSheet monsterSheet = new NXBitmapSheet(mMonsterTexture, 4, 4);
		for (int i = 0; i < mMonster.length; ++i) {
			mMonster[i] = new Monster(monsterSheet);
			mMonster[i].setDisplayFrame(0, 0);
			addChild(mMonster[i], i);
		}
		
		mDirectionKey = new NXMenuItemImage[MENU_ITEM_NUM];
		for (int j = 0; j < MENU_ITEM_NUM; ++j) {
			mDirectionKey[j] = 
				new NXMenuItemImage(mMenuSkin[0], mMenuSkin[1], mMenuSkin[2], null, new NXMenuItemActionHandler() {
					@Override
					public void onTapped(NXNode sender) {
						for (Monster monster : mMonster) {
							monster.recalcBehavior();
						}
					}

					@Override
					public void onPressed(NXNode sender) {
						Log.w("[NEOX]", "item " + this.toString() + "pressed.");
					}

					@Override
					public void onReleased(NXNode sender) {
						Log.w("[NEOX]", "item " + this.toString() + "released.");
					}
					
				});
			mDirectionKey[j].position(itemPos[j]);
		}
		mDirectionTray = new NXMenu(mDirectionKey);
		mDirectionTray.position(NXDirector.getScreenWidth() / 2, NXDirector.getScreenHeight() / 2);
		mDirectionTray.opacity(100);
		addChild(mDirectionTray);
		
		backgroundColor(Color.GRAY);
		mPaint.setColor(Color.BLUE);
	}

	private final void waitForChooseStartTerritory() {
		int left = NXDirector.getScreenWidth() / 3;
		int top = NXDirector.getScreenHeight() / 2;
		mStartRect.set(left, top, 
				left + (int)(Math.random() * MAX_START_RECT_SIZE), 
				top + (int)(Math.random() * MAX_START_RECT_SIZE));
	}
	
	@Override
	public void draw(Canvas canvas) {
		canvas.drawRect(mStartRect, mPaint);
		super.draw(canvas);
	}
	
	@Override
	public void update(int arg0) {
		if (mScreenTouched) {
			for (Monster monster : mMonster) {
				monster.step();
			}
		} else {
			waitForChooseStartTerritory();
		}
	}

	@Override
	public void onScreenSizeChanged(int newWidth, int newHeight) {
		Rect territory = new Rect(0, 0, newWidth, newHeight);
		mDirectionTray.position(50, newHeight / 3 * 2);
		Monster.territory(territory);
		for (Monster monster : mMonster) {
			monster.randomPosition(null); // TODO:
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
		/*if (MotionEvent.ACTION_DOWN == event.getAction()) {
			for (Monster monster : mMonster) {
				monster.target((int)event.getRawX(), (int)event.getRawY());
			}
		} else */if (MotionEvent.ACTION_UP == event.getAction()){
			mScreenTouched = true;
		}
		return super.onTouchEvent(event);
	}
}
